<!-- Toast.vue -->
<template>
  <div class="toast" v-show="isShow">
    <div>{{ message }}</div>
  </div>
</template>

<script>
export default {
  name: "Toast",
  data() {
    return {
      message: "",
      isShow: false,
    };
  },
  methods: {
    // 当使用这个方法后，将isShow改为true并且显示内容
    show(message, duration = 1000) {
      this.isShow = true;
      this.message = message;
      // 持续显示时间后isShow改为false，清空内容
      setTimeout(() => {
        this.isShow = false;
        this.message = "";
      }, duration);
    },
  },
};
</script>

<style>
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  padding: 8px 10px;
  border-radius: 4px;
  transform: translate(-50%, -50%);
  color: #fff;
  background-color: rgba(0, 0, 0, 0.8);
  z-index: 999;
}
</style>
